package com.hospital.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hospital.dao.*;
import com.hospital.model.*;
import com.hospital.util.*;


@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request,HttpServletResponse response) 
			throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request,HttpServletResponse response) 
			throws ServletException, IOException {
		String select = request.getParameter("selectName");
		if("bing".equals(select)){
			PatientInfo patientInfo = new PatientInfo();
			String account = request.getParameter("account").trim();
			patientInfo.setAccount(account);
			String pswd = request.getParameter("pswd").trim();
			String md5password = MD5.MD5(pswd);
			patientInfo.setPswd(md5password);
			PatientInfoDAO patientInfoDAO = new PatientInfoDAO();
			PatientInfo patientLoginInfo = patientInfoDAO.login(patientInfo);
			if (patientLoginInfo.getAccount() != null) {
				HttpSession session = request.getSession();
				session.setMaxInactiveInterval(3 * 60 * 60);// 设置session有效期为三小时
				session.setAttribute("patientInfo", patientLoginInfo);
				String sqlsuccess = "insert into t_login_time(account, logintime, state, root) "+"values('" 
						+ account + "', '" + DateUtil.getDateTime("yyyyMMddHHmmss") + "','" + "登陆成功" + "', '" + "病人" +"')";
				DbUtil.add_modify(sqlsuccess);
				response.sendRedirect(request.getServletContext().getContextPath() + "/manage/main.jsp");
			} else {
				request.setAttribute("rs", "<font color='red'>确认角色、用户名、密码是否正确，重新登陆</font>");
				String sqlfail = "insert into t_login_time(account, logintime, state, root) "+"values('" 
						+ account + "', '" + DateUtil.getDateTime("yyyyMMddHHmmss") + "','" + "登陆失败" + "', '" + "病人" +"')";
				DbUtil.add_modify(sqlfail);
				this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
			}
		}else if("gong".equals(select)){
			UserInfo userInfo = new UserInfo();
			String account = request.getParameter("account").trim();
			userInfo.setAccount(account);
			String pswd = request.getParameter("pswd").trim();
			String md5password = MD5.MD5(pswd);
			userInfo.setPswd(md5password);
			UserInfoDAO userInfoDAO = new UserInfoDAO();
			UserInfo userLoginInfo = userInfoDAO.login(userInfo);
			if (userLoginInfo.getAccount() != null) {
				HttpSession session = request.getSession();
				session.setMaxInactiveInterval(3 * 60 * 60);// 设置session有效期为三小时
				session.setAttribute("userInfo", userLoginInfo);
				String sqlsuccess = "insert into t_login_time(account, logintime, state, root) "+"values('" 
						+ account + "', '" + DateUtil.getDateTime("yyyyMMddHHmmss") + "','" + "登陆成功" + "', '" + "工作人员" +"')";
				DbUtil.add_modify(sqlsuccess);
				response.sendRedirect(request.getServletContext().getContextPath() + "/manage/main.jsp");
			} else {
				request.setAttribute("rs","<font color='red'>确认角色、用户名、密码是否正确，重新登陆</font>");
				String sqlfail = "insert into t_login_time(account, logintime, state, root) "+"values('" 
						+ account + "', '" + DateUtil.getDateTime("yyyyMMddHHmmss") + "','" + "登陆失败" + "', '" + "病人" +"')";
				DbUtil.add_modify(sqlfail);
				this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
			}
		}else{
			request.setAttribute("rs", "确认角色、用户名、密码是否正确，重新登陆");
			this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
			
		}
	}
		
}
